package com.zhangkunhao.notes.web.filter; /**
 * @description TODO
 * @author 20642
 * @date 2023/5/2 15:46
 */

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/*")
public class AllFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        // 强转获取HttpServletRequest对象
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        // 获取Session对象
        HttpSession session = httpRequest.getSession();
        // 获取Session中保存的数据,可能是null
        Integer userId = (Integer) session.getAttribute("userId");
        String administratorAccount = (String) session.getAttribute("administratorAccount");
        String administratorPassword = (String) session.getAttribute("administratorPassword");

        System.out.println("拦截所有的filter的userId->" + userId);
        //公共放行的资源
        String[] publicUrls = {
                "/index.html",
                "/js/",
                "/element-ui/",
                "/uploads/",

                "/user/login",
                "/user/register",
                "/user/forgetPassword",
                "/user/sendEmailCode",
                "/user/showPost",
                "/checkCodeServlet",
                "/user/selectByPage",

                "/administrator/administratorLogin",
        };

        //管理员的资源
        String[] adminUrls = {
                "/user/banUser",
                "/user/cancelBanUser",
                "/question/listUncheckedQuestions",
                "/question/agreedPublishQuestion",
                "/question/sendRejectQuestion",
                "/question/listReportedQuestions",
                "/question/disagreedReportQuestion",
                "/question/agreedReportQuestion",
                "/answerComment/listReportedComments",
                "/answerComment/agreedReportComment",
                "/answerComment/disagreedReportComment",
                "/user/listUsers",
                "/permissionBlacklist/listUserPrivileges",
                "/permissionBlacklist/setUserPrivilege",
                "/permissionBlacklist/cancelSetUserPrivilege",

        };

        //当前访问url
        String url = httpRequest.getRequestURL().toString();
        System.out.println("url=" + url);

        for (String u : publicUrls) {
            if (url.contains(u)) {
                //公共资源放行
                chain.doFilter(request, response);
                return;
            }
        }


        if (userId != null) {
            for (String adminUrl : adminUrls) {
                //用户相关操作
                if(!url.contains(adminUrl)){
                    //放行
                    chain.doFilter(request, response);
                    return;
                }
            }
        }


        if(administratorAccount!=null&&administratorPassword!=null){
            for (String adminUrl : adminUrls) {
                //管理员相关操作
                if(url.contains(adminUrl)){
                    //放行
                    chain.doFilter(request, response);
                    return;
                }
            }
        }
        System.out.println(3);

        //拦截
        response.getWriter().write("Please login first!");
        response.getWriter().flush();

    }

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void destroy() {
    }

}
